package com.qf.student.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @version1.0
 * @auth dmh
 */
@WebFilter(urlPatterns = "/*")
public class EncodingFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("EncodingFilter.init");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

       // 1.类型转换
       HttpServletRequest req= (HttpServletRequest) servletRequest;
        HttpServletResponse resp= (HttpServletResponse) servletResponse;
        //2.设置编码
        req.setCharacterEncoding("utf-8");//这是请求内容的编码
        ///resp.setContentType("utf-8");
        resp.setContentType("text/html;charset=utf-8");//设置内容的编码

        //3.放行 filterChain过滤链
        filterChain.doFilter(req,resp);
    }

    @Override
    public void destroy() {

    }
}
